package com.fastloan.mgt.verify.service.impl;

import com.fastloan.mgt.application.base.enums.ApplicationStatusEnum;
import com.fastloan.mgt.application.base.model.Application;
import com.fastloan.mgt.application.base.service.ApplicationService;
import com.fastloan.mgt.processcontrol.base.annotation.FunctionHandlerClass;
import com.fastloan.mgt.processcontrol.base.annotation.FunctionHandlerMethod;
import com.fastloan.mgt.processcontrol.base.model.ProcessSchedule;
import com.fastloan.mgt.processcontrol.base.model.ProcessSession;
import com.fastloan.mgt.processcontrol.base.service.ProcessDispatcher;
import com.fastloan.mgt.product.flow.enums.ProductFunctionGroup;
import com.fastloan.mgt.verify.model.GeneralVerifyResult;
import com.fastloan.mgt.verify.model.tobacco.TobaccoICertificate;
import com.fastloan.mgt.verify.service.TobaccoICertificateVerifyService;
import com.fastloan.mgt.verify.service.VerifyService;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yanwe
 * createTime 2017-12-2017/12/18 10:44
 */
@Service
@FunctionHandlerClass(processName = "烟草证认证", bmpIds = {ProductFunctionGroup.GROUP_ONE})
public class TobaccoICertificateVerifyImpl implements TobaccoICertificateVerifyService {

    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private VerifyService verifyService;

    @Override
    @FunctionHandlerMethod
    public void tobaccoICertificateVerifyProcess(String processId){
        String applicationId = ProcessDispatcher.getProcessSession(processId)
                .getAttribute("applicationId", String.class);
        applicationService.modifyApplicationStatus(applicationId, ApplicationStatusEnum.TOBACCOL_VERIFY);
    }

    @Override
    public ResultTo tobaccoICertificateVerify(String applicationId, TobaccoICertificate tobaccoICertificate) {
        Application application = applicationService.selectByPrimaryKey(applicationId);
        if (!ProcessDispatcher.getCurrentProcess(application.getProcessId()).getProcessClass()
                .equalsIgnoreCase(TobaccoICertificateVerifyImpl.class.getSimpleName())){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"流程错误,该进件未进行到该步骤");
        }
        GeneralVerifyResult generalVerifyResult = verifyService.tobaccoVerify(tobaccoICertificate);
        if (!generalVerifyResult.isSuccess()){
            return new ResultTo(ResultEnum.OPERATION_FAILED,generalVerifyResult.getMsg());
        }
        ProcessSession processSession = ProcessDispatcher.getProcessSession(application.getProcessId());
        processSession.setAttribute("tobaccoVerifyId",generalVerifyResult.getData());
        ProcessDispatcher.executeEnd(application.getProcessId(), true);
        ProcessSchedule currentProcess = ProcessDispatcher.getCurrentProcess(application.getProcessId());
        return new ResultTo().setData("nextProcess",currentProcess);
    }
}
